home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 352_01 / vecz.cpp < prev    next >
C/C++ Source or Header  |  1991-04-22  |  3KB  |  157 lines

  1. //////// COMPLEX VECTORS
  2. //
  3. //        primary subroutines to support complex vector math :
  4. //        assignment, addition, and mutliplication.
  5. //
  6. #include <stdlib.h>
  7. #include <alloc.h>
  8. #include <string.h>
  9. #include "wtwg.h"
  10.  
  11. #include "dblib.h"
  12.  
  13. #include "vector.h"
  14.  
  15.  
  16.  
  17.     CVector& CVector::operator=(CVector& vec)
  18.         // assign  CVector to Cvector
  19.         {
  20.         ispolar = vec.ispolar;
  21.         x = vec.x;
  22.         y = vec.y;
  23.         return *this;
  24.         }
  25.  
  26.     CVector& CVector::operator=(Vector& vec)
  27.         // assign real V. to CV
  28.         {
  29.         x = vec;
  30.         y =0;
  31.         ispolar = 0;    
  32.         return *this;
  33.         }
  34.  
  35.     CVector& CVector::operator=(float f)
  36.         // assign float f to CVvector
  37.         {
  38.         x =f;
  39.         y =0;
  40.         ispolar =0;
  41.         return *this;
  42.         }
  43.  
  44.  
  45.     CVector& CVector::operator=(complex z)
  46.         // assign comlex z to CVvector
  47.         {
  48.         x = real (z);
  49.         y = imag (z);
  50.         ispolar = 0;
  51.         return *this;
  52.         }
  53.  
  54.  
  55.     CVector& CVector::operator+=(CVector&  vec)
  56.         // add CVector vec to CVector this.
  57.         {
  58.         if ( ispolar || vec.ispolar )
  59.             {
  60.             werror ( 'V', "ADDING POLAR VECTORS" );
  61.             }
  62.         
  63.         x += vec.x;
  64.         y += vec.y;
  65.         
  66.         return *this;
  67.         }
  68.  
  69.     CVector& CVector::operator+=(Vector&  vec)
  70.         // add real Vector vec to CVector this.
  71.         {
  72.         if ( ispolar )
  73.             {
  74.             werror ( 'V', "ADDING POLAR VECTORS" );
  75.             }
  76.         x += vec;
  77.         return *this;
  78.         }
  79.  
  80.  
  81.     CVector& CVector::operator+=(float  f)
  82.         // add float f to Vector this.
  83.         {
  84.         if ( ispolar ) werror ( 'V', "ADDING POLAR VECTORS" );
  85.         
  86.         x += f;
  87.         
  88.         return *this;
  89.         }
  90.  
  91.     CVector& CVector::operator+=(complex z)
  92.         // add complex z to Vector this.
  93.         {
  94.         if ( ispolar ) werror ( 'V', "ADDING POLAR VECTORS" );
  95.         
  96.         x += real (z);
  97.         y += imag (z);
  98.         
  99.         return *this;
  100.         }
  101.  
  102.  
  103.     CVector& CVector::operator*=(CVector&  z)
  104.         // multiply CVector this by CVector z.
  105.         {
  106.         if ( ispolar )
  107.             {
  108.             if ( z.ispolar )
  109.                 {
  110.                 x*= z.x;        // mult mags, add phases
  111.                 y+= z.y;
  112.                 }
  113.             else    werror ('V', "VECTOR MULTIPLY mixing rect and polar");
  114.             }
  115.         else
  116.             {
  117.             if ( ! z.ispolar )
  118.                 {
  119.                 x *= z.x;
  120.                 y *= z.y;
  121.                 }
  122.             else    werror ('V', "VECTOR MULTIPLY mixing rect and polar");
  123.             }
  124.         return *this;
  125.         }
  126.  
  127.  
  128.     CVector& CVector::operator*=(float  f)
  129.         // Multiply CVector this by float f
  130.         // note works for either polar or rect.
  131.         {
  132.         
  133.         x *= f;
  134.         if ( ! ispolar )  y *= f;
  135.         else  this->rectify();        // make sure mag.s are all positive.
  136.         return *this;
  137.         }
  138.  
  139.  
  140.     CVector& CVector::operator*=(complex z)
  141.         // Multiply CVector this by scalar complex z
  142.         {
  143.         if ( ispolar ) 
  144.             {
  145.             float  zmag = norm (z);
  146.             x *= ( ( zmag > SMALL_VAL ) ? sqrt(zmag) : 2*zmag );
  147.             y += arg (z);
  148.             }
  149.         else
  150.             {
  151.             x *= real (z);        // rect. form of mutilply.
  152.             y *= imag (z);        
  153.             }    
  154.         return *this;
  155.         }
  156. //----------------- end VECZ.CPP-------------------------    
  157.